package infopreserve.main.mobile;

import infopreserve.main.mobile.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Spinner;


public class OCR extends Activity{
	public CameraView cameraView;
	public Bitmap pic;
	public Camera camera;
	public Dialog dialog;
	private FrameLayout preview;
	public ArrayAdapter<String> adapter;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ocrlayout);
		cameraView = new CameraView(this);
		preview = (FrameLayout)findViewById(R.id.preview);
		preview.addView(cameraView);
	}
	public void takePic(View v){
		camera = cameraView.camera;
		camera.takePicture(null, null, p);
	}

	PictureCallback p = new PictureCallback(){
		public void onPictureTaken(byte[] picture, Camera c) {
			dialog = new Dialog(OCR.this);
			dialog.setContentView(R.layout.picturepreview);
			final Button button = (Button) dialog.findViewById(R.id.button1);
			Button b = (Button) dialog.findViewById(R.id.button2);
			dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
			dialog.setTitle("New OCR File");
			dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			dialog.show();
			ImageView picview = (ImageView)dialog.findViewById(R.id.imageView1);
			Bitmap pic = BitmapFactory.decodeByteArray(picture, 0, picture.length);
			int x = preview.getWidth();
			int y = preview.getHeight();
			Bitmap scaledBitmap = Bitmap.createScaledBitmap(pic, y, x, true);
			Matrix matrix = new Matrix();
			matrix.postRotate(90);
			Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
			picview.setImageBitmap(rotatedBitmap);
			Spinner s = (Spinner)dialog.findViewById(R.id.spinner1);
			adapter = new ArrayAdapter<String>(OCR.this, android.R.layout.simple_spinner_item);
			adapter.add("Sample Category");
			s.setAdapter(adapter);
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					//add upload stuff
				}
			});

			b.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					if(camera != null)
						camera.startPreview();
					else
						camera.open();

				}
			});
			dialog.setOnDismissListener(new DialogInterface.OnDismissListener()
			{
				public void onDismiss(DialogInterface dialog)
				{
					dialog.dismiss();
					if(camera != null)
						camera.startPreview();
					else
						camera.open();
				}
			});
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			//method here
			return true;
		case R.id.info:
			Intent intent = new Intent();
			intent.setClass(this,Info.class);
			startActivity(intent);
			return true;
        case R.id.signout:
 		     Intent i = new Intent();
 		     i.setClass(this,InfoPreserveActivity.class);
 		     startActivity(i);
 		     this.finish();
            return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	public void onBackPressed(){
		return;
	}
	
	
}